package com.weixin.project.Controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weixin.project.Annotation.VipAccess;
import com.weixin.project.Entity.Chapter;
import com.weixin.project.Entity.Question;
import com.weixin.project.Entity.User;
import com.weixin.project.Service.QuestionService;
import com.weixin.project.Service.RecommendService;
import com.weixin.project.Service.UserService;
import com.weixin.project.Util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class RecommendController {
    @Autowired
    RecommendService recommendService;
    @Autowired
    QuestionService questionService;
    @Autowired
    UserService userService;
    @GetMapping("/test/similar_questions")
    JSONObject getSimilarQuestions(@RequestParam("id") Integer questionId){
        JSONObject ret = new JSONObject();
        List<Question> similarQuestions = recommendService.findSimilarQuestionsList(questionId);
        JSONArray arr = new JSONArray();
        for (Question similarQuestion :
                similarQuestions) {
            JSONObject obj = new JSONObject();
            obj.put("question_id", similarQuestion.getId());
            obj.put("question_content", similarQuestion.getStem());
            Chapter chapter = questionService.findChapter(similarQuestion.getChapterId());
            obj.put("chapter_id", chapter.getId());
            obj.put("chapter_name", chapter.getChapterName());
            arr.add(obj);
        }
        ret.put("similar_questions", arr);
        return ResponseUtil.success("", ret);
    }

    @GetMapping("/test/random_recommend")
    JSONObject randomRecommend(HttpServletRequest request) {
        JSONObject ret = new JSONObject();
        User currentUser = userService.findCurrentUser(request);
        List<Question> questions = recommendService.recommend(currentUser.getId());
        JSONArray array = new JSONArray();
        for (Question question: questions) {
            JSONObject obj = new JSONObject();
            obj.put("stem", question.getStem());
            obj.put("explain", question.getExplanation());
            obj.put("score", question.getScore());
            obj.put("difficulty", question.getDifficulty());
            obj.put("type_id", question.getTypeId());
            obj.put("id", question.getId());
            array.add(obj);
        }
        ret.put("questions", array);
        return ResponseUtil.success("", ret);
    }

    @VipAccess
    @PostMapping("/recommend/score")
    JSONObject scoreRec(HttpServletRequest request){
        User currentUser = userService.findCurrentUser(request);
        return null;
    }
}
